#include <bits/stdc++.h>
using namespace std;

struct Station {
    int h, v;
};

int main() {
    int n;
    cin >> n;
    vector<Station> stations(n);
    for (int i = 0; i < n; ++i) {
        cin >> stations[i].h >> stations[i].v;
    }

    vector<int> receive(n, 0);
    stack<int> st;

    // 找左边第一个更高的发射站
    for (int i = 0; i < n; ++i) {
        while (!st.empty() && stations[st.top()].h <= stations[i].h) {
            st.pop();
        }
        if (!st.empty()) {
            receive[st.top()] += stations[i].v;
        }
        st.push(i);
    }

    // 清空栈，找右边第一个更高的发射站
    while (!st.empty()) st.pop();
    for (int i = n - 1; i >= 0; --i) {
        while (!st.empty() && stations[st.top()].h <= stations[i].h) {
            st.pop();
        }
        if (!st.empty()) {
            receive[st.top()] += stations[i].v;
        }
        st.push(i);
    }

    int max_energy = 0;
    for (int val : receive) {
        max_energy = max(max_energy, val);
    }
    cout << max_energy << endl;

    return 0;
}